package com.winit.flow.repository;

import com.winit.flow.model.FlowReqRelation;
import com.winit.flow.model.FlowReqRelationId;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import javax.transaction.Transactional;
import java.util.List;

public interface FlowReqRelationRepository extends CrudRepository<FlowReqRelation, FlowReqRelationId> {
    List<FlowReqRelation> findByFlowId(String flowId);
    List<FlowReqRelation> findByReqId(String reqId);


    @Modifying
    @Transactional
    @Query(value = "update matrix_flow_req set status = ?1 where flow_id = ?2 and req_id = ?3 and if(?4 is null, version is null, version = ?4)", nativeQuery = true)
    int updateFlowStatusByReqIdAndFlowId(String status, String flowId, String reqId, String version);

    FlowReqRelation findByFlowIdAndReqId(String flowId, String reqId);

    List<FlowReqRelation> findByFlowIdAndStatusNot(String flowId, String status);
}
